package com.example.todolist.mapper;

import com.example.todolist.entity.Task;
import org.apache.ibatis.annotations.*;

import java.sql.Date;
import java.util.List;

@Mapper
public interface TaskMapper {
    @Insert("insert into task(taskName,userId,beginDate,endDate,description,taskDone,taskType)VALUES(#{taskName},#{userId},#{beginDate},#{endDate},#{description},#{taskDone},#{taskType})")
    int insertByTask(Task task);
    @Select("select count(*) from task where taskName=#{taskName} AND userId=#{userId}")
    Integer selectSameName(Integer userId,String taskName);
    @Delete("delete from task where userId=#{userId} AND taskId=#{taskId}")
    Integer deleteByTaskId(Integer userId,Integer taskId);
    @Select("select * from task where taskName=#{taskName} AND userId=#{userId}")
    Task findByTaskName(Integer userId,String taskName);
    Integer update(Task task);         //更新task信息
    @Select("select count(*) from task where userId=#{userId} AND taskDone=1")
    Integer countTheTaskDone(Integer userId);

    @Select("select count(*) from task where userId=#{userId} AND taskDone=0")
    Integer countTheTaskNot(Integer userId);

    @Select("select * from task where userId=#{userId}")
    List<Task> findByUserId(Integer userId);

    @Select("select * from task where taskDone=0 AND (year(beginDate)<#{year} or year(beginDate)=#{year} AND month(beginDate)<=#{month}) and (year(endDate)>#{year} or year(endDate)=#{year} and month(endDate)>=#{month}) AND userId=#{userId};")
    List<Task> findMonthTask(Integer userId,Integer year,Integer month);

    @Update("update task set taskDone=1,doneDate=#{date} where userId=#{userId} AND taskId=#{taskId}")
    void checkForTask(Integer userId,Integer taskId,Date date);

    @Select("select * from task where userId=#{userId} AND beginDate<=#{date} AND endDate>=#{date} AND taskDone=0")
    List<Task> getOneDayTask(Integer userId, Date date);

    @Select("select taskName from task where userId=#{userId} AND date_sub(curdate(),interval 1 day) <= doneDate")
    List<String> getLastDayTask(Integer userId);

    @Select("select * from task where userId=#{userId}")
    List<Task> getALLTask(Integer userId);

    @Select("select * from task where userId=#{userId} AND taskType like #{taskType}")
    List<Task> getTypeTask(Integer userId, String taskType);

}
